public class Rectangle extends ClosedFigure
{
    public double length, width;

    public Rectangle()
    {
        this(new Point(),new Point());
    }

    public Rectangle(Point p1,Point p2)
    {
        super("矩形",new Point(Math.min(p1.getX(),p2.getX()),Math.min(p1.getY(),p2.getY())));
        this.length=Math.max(p1.getX(),p2.getX())-Math.min(p1.getX(),p2.getX());
        this.width=Math.max(p1.getY(),p2.getY())-Math.min(p1.getY(),p2.getY());
    }

    public Rectangle(Point p1,double length,double width)
    {
        this(p1,length,width,false);
    }

    public Rectangle(Point p1,double length,double width,boolean mustPositive)
    {
        this(p1,new Point(p1.getX()+length,p1.getY()+width));
        if(mustPositive&&(length<0||width<0))//这里不能用带this的,因为我的构造函数太先进了,会给他自己转过来...
            throw new IllegalArgumentException("Argument must be positive but "+((length<=0)?("length("+length+") is negative "):"")+((width<=0)?("width("+width+") is negative "):""));
    }

    public Rectangle(Rectangle r)
    {
        this(r.point1,r.length,r.width);
    }

//    public String toString()
//    {
//        return "Rectangle{"+
//                "shape='"+shape+'\''+
//                ", point1="+point1+
//                ", length="+length+
//                ", width="+width+
//                '}';
//    }

    public String toString()
    {
        return "new Rectangle("+point1+","+length+","+width+")";
    }

    public double perimeter()
    {
        return (width+length)*2;
    }

    public double area()
    {
        return width*length;
    }

    public Rectangle revolve(double angle)
    {
        return revolve(new Point(point1.getX()+length/2,point1.getY()+width/2),angle);
    }

    public Rectangle revolve(Point p,double angle)
    {
        return new Rectangle(this.point1.revolve(p,angle),new Point(point1.getX()+length,point1.getY()+width).revolve(p,angle));
    }

    public Rectangle zoom(double percentage)
    {
        return zoom(new Point(point1.getX()+length/2,point1.getY()+width/2),percentage);
    }

    public Rectangle zoom(Point p,double percentage)
    {
        return new Rectangle(this.point1.zoom(p,percentage),new Point(point1.getX()+length,point1.getY()+width).zoom(p,percentage));
    }

    public boolean contains(Point p)
    {
        Point[] ps={point1,new Point(point1.getX()+length,point1.getY()),new Point(point1.getX()+length,point1.getY()+width),new Point(point1.getX(),point1.getY()+width)};
        return new Polygon(ps).contains(p);
    }
}
